package com.jltx.zeus.view

import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.jltx.zeus.viewmodel.ActionEventObserver
import kotlinx.coroutines.CoroutineScope

/**
 * @author jltxseo
 *         Created by junlintianxia on 2020/05/08.
 */
abstract class BaseActivity : AppCompatActivity(), ActionEventObserver {
    override val viewModelScope: CoroutineScope
        get() = lifecycleScope

    override val lContext: Context?
        get() = this

    override val lLifecycleOwner: LifecycleOwner
        get() = this

    protected abstract val layoutId: Int

    protected val layoutView: View?
        get() = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        initContentLayout()
        if (init()) {
            initView(savedInstanceState)
            initListener()
            initData(savedInstanceState)
        } else {
            finish()
        }
    }

    private fun initContentLayout() {
        val layoutId = layoutId
        if (layoutId > 0 || layoutView != null) {

            if (layoutId > 0) {
                setContentView(layoutId)
            } else {
                setContentView(layoutView)
            }
        }
    }

    protected open fun init(): Boolean {
        return true
    }

    override fun showLoading(msg: String) {

    }

    override fun dismissLoading() {

    }

    override fun finishView() {
        this.finish()
    }

    protected open fun initView(savedInstanceState: Bundle?) {}

    protected open fun initData(savedInstanceState: Bundle?) {}

    protected open fun initListener() {}
}